package com.ct.application.coupon.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.coupon.convert.CouponConvert;
import com.ct.application.coupon.model.po.CouponAddPO;
import com.ct.application.coupon.model.po.CouponQueryPO;
import com.ct.application.coupon.model.po.CouponUpdatePO;
import com.ct.application.coupon.model.vo.CouponVO;
import com.ct.coupon.orm.Coupon;
import com.ct.coupon.responsity.ICouponRepository;
import com.ct.repository.commons.PageUtils;
import com.ct.utils.valid.ValidatorUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 商品表 服务实现类
 *
 * @author system
 */
@Service
public class CouponService {


    @Resource
    private ICouponRepository couponRepository;
    @Resource
    private CouponConvert couponConvert;


    public boolean updateStatus(StatusRequest statusRequest){
        if(ValidatorUtil.isNumeric(statusRequest.getValue())) {
            return couponRepository.updateStatus(statusRequest.getId(), statusRequest.getField(), Integer.parseInt(statusRequest.getValue()));
        }else{
            return couponRepository.updateStatusStr(statusRequest.getId(), statusRequest.getField(), statusRequest.getValue());
        }
    }

    /**
     * 获取商品列表数据
     */
    public PageUtils<CouponVO> getCouponList(CouponQueryPO queryPO) {
        LambdaQueryWrapper<Coupon> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(queryPO.getKeyword()), Coupon::getTitle, queryPO.getKeyword());
        lambdaQueryWrapper.eq(!Objects.isNull(queryPO.getStatus()), Coupon::getStatus, queryPO.getStatus());
        lambdaQueryWrapper.orderByDesc(Coupon::getCreateTime);
        Page<Coupon> page = new Page<>(queryPO.getPageNo(), queryPO.getPageSize());
        IPage<Coupon> couponIPage = couponRepository.page(page, lambdaQueryWrapper);

        List<Coupon> couponList = couponIPage.getRecords();
        if (couponList.isEmpty()) {
            return PageUtils.getEmptyInstance();
        }
        List<CouponVO> couponVOList = couponConvert.toCouponVOList(couponList);
        return new PageUtils<>(couponIPage, couponVOList);
    }


    /**
     * 根据id查询商品
     */
    public CouponVO getCouponById(Long id) {
        Coupon coupon = couponRepository.getById(id);
        if (coupon == null) {
            return null;
        }
        CouponVO couponVO = new CouponVO();
        BeanUtils.copyProperties(coupon,couponVO);
        return couponVO;
    }


    /**
     * 新增商品
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean add(CouponAddPO addPO) {
        Coupon coupon = new Coupon();
        BeanUtils.copyProperties(addPO, coupon);
        return coupon.insert();
    }


    /**
     * 修改商品
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean update(CouponUpdatePO updatePO) {
        Coupon coupon = new Coupon();
        BeanUtils.copyProperties(updatePO, coupon);
        return coupon.updateById();
    }


    /**
     * 删除商品
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return couponRepository.removeById(id);
    }

}
